home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / comint / background.el.z / background.el
Encoding:
Text File  |  1998-05-21  |  5.1 KB  |  138 lines

  1. ;;; background.el --- fun with background jobs
  2.  
  3. ;; Copyright (C) 1988 Joe Keane <jk3k+@andrew.cmu.edu>
  4. ;; Keywords: processes
  5.  
  6. ;; This file is part of XEmacs.
  7.  
  8. ;; XEmacs is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 2 of the License, or
  11. ;; (at your option) any later version.
  12.  
  13. ;; XEmacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ;; GNU General Public License for more details.
  17.  
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with XEmacs; if not, write to the Free Software
  20. ;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  21. ;; 02111-1307, USA.
  22.  
  23. ;;; Synched up with: Not in FSF
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; - Adapted to use comint and cleaned up somewhat. Olin Shivers 5/90
  28. ;; - Background failed to set the process buffer's working directory
  29. ;;   in some cases. Fixed. Olin 6/14/90
  30. ;; - Background failed to strip leading cd's off the command string
  31. ;;   after performing them. This screwed up relative pathnames.
  32. ;;   Furthermore, the proc buffer's default dir wasn't initialised 
  33. ;;   to the user's buffer's default dir before doing the leading cd.
  34. ;;   This also screwed up relative pathnames if the proc buffer already
  35. ;;   existed and was set to a different default dir. Hopefully we've
  36. ;;   finally got it right. The pwd is now reported in the buffer
  37. ;;   just to let the user know. Bug reported by Piet Van Oostrum.
  38. ;;   Olin 10/19/90
  39. ;; - Fixed up the sentinel to protect match-data around invocations.
  40. ;;   Also slightly rearranged the cd match code for similar reasons.
  41. ;;   Olin 7/16/91
  42. ;; - Dec 29 1995: changed for new stuff (shell-command-switch, second
  43. ;;   arg to shell-command --> BUFFER-NAME arg to background) from
  44. ;;   FSF 19.30.  Ben Wing
  45.  
  46. ;;; Code:
  47.  
  48. (provide 'background)
  49. (require 'comint)
  50.  
  51. (defgroup background nil
  52.   "Fun with background jobs"
  53.   :group 'processes)
  54.  
  55.  
  56. ;; user variables
  57. (defcustom background-show t
  58.   "*If non-nil, background jobs' buffers are shown when they're started."
  59.   :type 'boolean
  60.   :group 'background)
  61. (defcustom background-select nil
  62.   "*If non-nil, background jobs' buffers are selected when they're started."
  63.   :type 'boolean
  64.   :group 'background)
  65.  
  66. ;;;###autoload
  67. (defun background (command &optional buffer-name)
  68.   "Run COMMAND in the background like csh.  
  69. A message is displayed when the job starts and finishes.  The buffer is in
  70. comint mode, so you can send input and signals to the job.  The process object
  71. is returned if anyone cares.  See also comint-mode and the variables
  72. background-show and background-select.
  73.  
  74. Optional second argument BUFFER-NAME is a buffer to insert the output into.
  75. If omitted, a buffer name is constructed from the command run."
  76.   (interactive "s%% ")
  77.   (let ((job-number 1)
  78.         job-name
  79.     (dir default-directory))
  80.     (while (get-process (setq job-name (format "background-%d" job-number)))
  81.       (setq job-number (1+ job-number)))
  82.     (or buffer-name
  83.     (setq buffer-name (format "*%s*" job-name)))
  84.     (if background-select (pop-to-buffer buffer-name)
  85.       (if background-show (with-output-to-temp-buffer buffer-name)) ; cute
  86.       (set-buffer (get-buffer-create buffer-name)))
  87.     (erase-buffer)
  88.  
  89.     (setq default-directory dir) ; Do this first, in case cd is relative path.
  90.     (if (string-match "^cd[\t ]+\\([^\t ;]+\\)[\t ]*;[\t ]*" command)
  91.     (let ((dir (substring command (match-beginning 1) (match-end 1))))
  92.        (setq command (substring command (match-end 0)))
  93.        (setq default-directory
  94.          (file-name-as-directory (expand-file-name dir)))))
  95.  
  96.     (insert "--- working directory: " default-directory
  97.         "\n% " command ?\n)
  98.  
  99.     (let ((proc (get-buffer-process
  100.          (comint-exec buffer-name job-name shell-file-name
  101.                   nil (list shell-command-switch command)))))
  102.       (comint-mode)
  103.       ;; COND because the proc may have died before the G-B-P is called.
  104.       (cond (proc (set-process-sentinel proc 'background-sentinel)
  105.           (message "[%d] %d" job-number (process-id proc))))
  106.       (setq mode-name "Background")
  107.       proc)))
  108.  
  109.  
  110. (defun background-sentinel (process msg)
  111.   "Called when a background job changes state."
  112.   (let ((ms (match-data))) ; barf
  113.     (unwind-protect
  114.      (let ((msg (cond ((string= msg "finished\n") "Done")
  115.               ((string-match "^exited" msg)
  116.                (concat "Exit " (substring msg 28 -1)))
  117.               ((zerop (length msg)) "Continuing")
  118.               (t (concat (upcase (substring msg 0 1))
  119.                      (substring msg 1 -1))))))
  120.        (message "[%s] %s %s" (process-name process)
  121.             msg
  122.             (nth 2 (process-command process)))
  123.        (if (null (buffer-name (process-buffer process)))
  124.            (set-process-buffer process nil) ; WHY? Olin.
  125.            (if (memq (process-status process) '(signal exit))
  126.            (save-excursion
  127.              (set-buffer (process-buffer process))
  128.              (let ((at-end (eobp)))
  129.                (save-excursion
  130.              (goto-char (point-max))
  131.              (insert ?\n msg ? 
  132.                  (substring (current-time-string) 11 19) ?\n))
  133.                (if at-end (goto-char (point-max))))
  134.              (set-buffer-modified-p nil)))))
  135.       (store-match-data ms))))
  136.  
  137. ;;; background.el ends here
  138.